package study.chapter03.thread.operation;

//线程强制执行
public class TestThread5 {
    public static void main(String[] args) {

        //A线程
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                if (i % 2 == 0){
                    System.out.println("A线程将资源礼让出去，让其他线程先执行");
                    Thread.yield();
                    System.out.println("资源礼让完成，A线程拿回资源继续执行");
                }
                System.out.println("A线程：" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();



        //主线程
        for (int i = 1; i <= 10; i++) {
            System.out.println("主线程：" + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}

